package com.example.zhidao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhidao.dao.FansMapper;
import com.example.zhidao.dao.UserMapper;
import com.example.zhidao.model.po.Fans;
import com.example.zhidao.service.FansService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

@Service
public class FansServiceImpl extends ServiceImpl<FansMapper, Fans> implements FansService {

    @Resource
    FansMapper fansMapper;

    @Resource
    UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addfan(Fans fans) {
        try{
            fansMapper.insert(fans);
            userMapper.addfan(fans.getUid());
            userMapper.addconcern(fans.getFansid());
        }catch (Exception e){
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delfan(Fans fans) {
        try{
            fansMapper.deleteById(fans);
            userMapper.delfan(fans.getUid());
            userMapper.delconcern(fans.getFansid());
        }catch (Exception e){
            // 回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

        return true;
    }
}
